<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <el-form
      ref="formData"
      label-width="120px"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          placeholder="请输入姓名"
          style="width: 50%"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          placeholder="请输入手机号"
          style="width: 50%"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          placeholder="请选择入职时间"
          style="width: 50%"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          placeholder="请选择聘用形式"
          style="width: 50%"
        >
          <el-option
            v-for="obj in EmployeeEnum.hireType"
            :key="obj.id"
            :label="obj.value"
            :value="obj.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          placeholder="请输入工号"
          style="width: 50%"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          placeholder="请选择部门"
          style="width: 50%"
          @focus="getDepartments"
        />
        <!-- 树形控件 -->
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="list"
          :props="{ label: 'name' }"
          :default-expand-all="true"
          @node-click="nodeTree"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          placeholder="请选择转正时间"
          style="width: 50%"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import { addEmployee } from '@/api/employees'
import EmployeeEnum from '@/api/constant/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EmployeeEnum, // 聘用形式中的选项
      list: [], // 存放树形结构数据
      showTree: false, // 显示树形结构
      loading: false, // 进度条
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '员工姓名不能为空', trigger: 'blur' },
          { min: 1, max: 5, message: '员工姓名在1-5位之间' }
        ],
        mobile: [
          { required: true, message: '员工手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      this.list = tranListToTreeData(depts, '')
      this.loading = false
    },
    nodeTree(node) {
      this.showTree = false
      this.formData.departmentName = node.name
    },
    // 点击确认按钮验证表单新增员工
    async btnOk() {
      try {
        await this.$refs.formData.validate()
        await addEmployee(this.formData)
        await this.$parent.getEmployeeList()
        this.$message.success('新增员工成功')
        this.$parent.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    btnCancel() {
      this.formData = {}
      this.$refs.formData.resetFields()
      this.$emit('update:showDialog', false)
      this.showTree = false
    }
  }
}
</script>

<style></style>
